package com.freedy.concurrent.Monitors.patterns;

import lombok.extern.slf4j.Slf4j;

/**
 * 同步模式之顺序控制
 * 固定顺序
 * 使用wait notify实现
 * @author Freedy
 * @date 2021/4/22 21:43
 */
@Slf4j
public class SequenceControlFix {
    private static final Object LOCK=new Object();
    private static boolean t2Run=false;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (LOCK){
                while (!t2Run){
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.info("1");
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            synchronized (LOCK){
                log.info("2");
                t2Run=true;
                LOCK.notifyAll();
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
